package com.partner.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.partner.domain.ArticleShoucang;
import com.partner.domain.article;
import com.partner.response.TestSkillResponse;
import com.partner.util.DBUtil;
import com.partner.util.DateUtil;

/**
 * 有关考试技巧
 * 
 * @author Jinshuan
 *
 */
public class ArticleDao {
	QueryRunner queryRunner_self = new QueryRunner();
	QueryRunner queryRunner_auto = new QueryRunner(DBUtil.getDataSource());

	// 分页查询考试技巧
	public TestSkillResponse selectSkillByPage(int pageSize, int pageNow,
			String type) {
		TestSkillResponse testSkillResponse = new TestSkillResponse();
		String sql = "select * from article where type = ? limit ?,?";
		Object[] params = { type, (pageNow - 1) * pageSize, pageSize };
		List<article> skills = new ArrayList<article>();
		System.out.println((pageNow - 1) * pageSize + "--" + pageSize + "--"
				+ type);
		try {
			if (queryRunner_auto.query(sql, new BeanListHandler<article>(
					article.class), params) != null) {
				skills = queryRunner_auto.query(sql,
						new BeanListHandler<article>(article.class), params);

				testSkillResponse.setCode("2000");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			testSkillResponse.setCode("400");
			testSkillResponse.setDescription("查询出错");
		}
		// try {
		// queryRunner_auto.query(sql, new
		// BeanListHandler<article>(article.class), params);
		// } catch (SQLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		testSkillResponse.setSkills(skills);
		return testSkillResponse;
	}

	// 给文章的收藏表添加一条记录,返回对应的id
	public int insertArticleShoucang(ArticleShoucang articleShoucang) {
		String sql = "insert into article_shoucang(ref_id,phone,shoucang_time) values(?,?,?)";
		String shoucang_time = articleShoucang.getShoucang_time();
		if(shoucang_time==null){
			shoucang_time = DateUtil.dateToString3(new Date());
		}
		Object[] params = { articleShoucang.getRef_id(),
				articleShoucang.getPhone(),shoucang_time};
		try {
			if (queryRunner_auto.update(sql, params) > 0) {
				int shoucang_id = hasShouCang(articleShoucang) ;
					return shoucang_id;
			} else {
				return -1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	//给文章删除一条记录
	public boolean deleteArticleShouCang(int id){
		String sql = "delete from article_shoucang where id = ?";
		Object[] params = {id};
		try {
			if(queryRunner_auto.update(sql, params)>0){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	// c查询是否已经收藏了，返回收藏的id
	public int hasShouCang(ArticleShoucang articleShoucang) {
		String sql = "select * from article_shoucang where ref_id = ? and phone = ?";
		Object[] params = { articleShoucang.getRef_id(),
				articleShoucang.getPhone() };
		List<ArticleShoucang> articleShoucangs = null;
		try {
			articleShoucangs = queryRunner_auto
					.query(sql, new BeanListHandler<ArticleShoucang>(
							ArticleShoucang.class), params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (articleShoucangs != null && articleShoucangs.size() > 0) {
			return articleShoucangs.get(0).getId();
		} else {
			return -1;
		}
	}
	
	//查询对应用户的所有的文章的收藏记录
	public List<ArticleShoucang> selectArticleShoucangsByUser(String phone){
		String sql = "select * from article_shoucang where phone = ?";
		Object[] params = {phone};
		try {
			List<ArticleShoucang> articleShoucangs = queryRunner_auto.query(sql, new BeanListHandler<ArticleShoucang>(ArticleShoucang.class), params);
			if(articleShoucangs==null){
				articleShoucangs = new ArrayList<ArticleShoucang>();
			}
			return articleShoucangs;
		} catch (SQLException e) {
		}
		return new ArrayList<ArticleShoucang>();
		
	}
	
	//查询对应用户所收藏的文章或者帖子
	public List<article> selectShouCangArticle(String phone,int pageNow,int pageSize){
		String sql = "select article.* from article,article_shoucang where phone = ? and article.id = article_shoucang.ref_id order by  shoucang_time desc  limit ?,?";
		Object[] params = {phone,(pageNow-1)*pageSize,pageSize};
		try {
			List<article> articles = queryRunner_auto.query(sql, new BeanListHandler<article>(article.class), params);
			if(articles!=null){
				return articles;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
